require "Dialogs"

Page = 0
ItemSelected = ""

Arena.MoveToAndResize(320, 0, 565, 130, true, true)
Encounter.Call("RotateButton", {"Fight", 0})
Encounter.Call("RotateButton", {"Act", 0})
Encounter.Call("RotateButton", {"Item", 0})
Encounter.Call("RotateButton", {"Mercy", 0})
Encounter.Call("ButtonMoveTo", {"Fight", 87, -50})
Encounter.Call("ButtonMoveTo", {"Act", 240, -50})
Encounter.Call("ButtonMoveTo", {"Item", 400, -50})
Encounter.Call("ButtonMoveTo", {"Mercy", 555, -50})
Encounter.Call("StatusMoveTo", {350, 160.5})
Encounter.Call("PapsMoveTo", {310, 220})

Black = CreateSprite("Specials/1","BelowPlayer")
Black.xscale = 565/575
Black.MoveTo(Arena.x, Arena.y + 2.5)
Black.color = {0, 0, 0}
BtF = CreateSprite("Fake UI, KR and HP Library Sprites/FIGHT 0","BelowPlayer")
BtA = CreateSprite("Fake UI, KR and HP Library Sprites/ACT 0","BelowPlayer")
BtI = CreateSprite("Fake UI, KR and HP Library Sprites/ITEM 0","BelowPlayer")
BtM = CreateSprite("Fake UI, KR and HP Library Sprites/MERCY 0","BelowPlayer")
BtF.MoveTo(80, 300)
BtA.MoveTo(80, 240)
BtI.MoveTo(560, 300)
BtM.MoveTo(560, 240)

if Encounter["InButton"] == "Fight" then
	X = 41
	Y = 298
elseif Encounter["InButton"] == "Act" then
	X = 42
	Y = 238
elseif Encounter["InButton"] == "Item" then
	X = 521
	Y = 298
elseif Encounter["InButton"] == "Mercy" then
	X = 520
	Y = 238
end

Bones = {}
ShakeL = {}
Bones = {}
Shards = {}

Player.SetControlOverride(true)

function Update()
	shake = {}
	DialogUpdate()
	if not Status then
		if GetAlMightyGlobal("Idioma") == "Spanish" then
			Dialog("[noskip]No más puzzles.", {X = 91, Y = 120}, nil, true, 1)
		else
			Dialog("[noskip]No more puzzles.", {X = 91, Y = 120}, nil, true, 1)
		end
		Status = "Buttons"
	end
	if Status == "Buttons" then
		if Input.Up == 1 and Input.Down <= 0 and Y ~= 298 then
			Y = 298
			if X == 42 then
				X = 41
			elseif X == 520 then
				X = 521
			end
			Audio.PlaySound("pip")
		elseif Input.Down == 1 and Input.Up <= 0 and Y ~= 238 then
			Y = 238
			if X == 41 then
				X = 42
			elseif X == 521 then
				X = 520
			end
			Audio.PlaySound("pip")
		end
		if Input.Left == 1 and Input.Right <= 0 and X ~= 41 and X ~= 42 then
			if Y == 298 then
				X = 41
			elseif Y == 238 then
				X = 42
			end
			Audio.PlaySound("pip")
		elseif Input.Right == 1 and Input.Left <= 0 and X ~= 520 and X ~= 521 then
			if Y == 298 then
				X = 521
			elseif Y == 238 then
				X = 520
			end
			Audio.PlaySound("pip")
		end
		if X == 41 and Y == 298 then
			BtF.Set("Fake UI, KR and HP Library Sprites/FIGHT 1")
			BtA.Set("Fake UI, KR and HP Library Sprites/ACT 0")
			BtI.Set("Fake UI, KR and HP Library Sprites/ITEM 0")
			BtM.Set("Fake UI, KR and HP Library Sprites/MERCY 0")
		elseif X == 42 and Y == 238 then
			BtF.Set("Fake UI, KR and HP Library Sprites/FIGHT 0")
			BtA.Set("Fake UI, KR and HP Library Sprites/ACT 1")
			BtI.Set("Fake UI, KR and HP Library Sprites/ITEM 0")
			BtM.Set("Fake UI, KR and HP Library Sprites/MERCY 0")
		elseif X == 521 and Y == 298 then
			BtF.Set("Fake UI, KR and HP Library Sprites/FIGHT 0")
			BtA.Set("Fake UI, KR and HP Library Sprites/ACT 0")
			BtI.Set("Fake UI, KR and HP Library Sprites/ITEM 1")
			BtM.Set("Fake UI, KR and HP Library Sprites/MERCY 0")
		elseif X == 520 and Y == 238 then
			BtF.Set("Fake UI, KR and HP Library Sprites/FIGHT 0")
			BtA.Set("Fake UI, KR and HP Library Sprites/ACT 0")
			BtI.Set("Fake UI, KR and HP Library Sprites/ITEM 0")
			BtM.Set("Fake UI, KR and HP Library Sprites/MERCY 1")
		end
	elseif Status == "ItemMenu" then
		if ((Input.Up == 1 and Input.Down <= 0) or (Input.Down == 1 and Input.Up <= 0)) and #Encounter["Items"] > 2 + 4*Page then
			if #Encounter["Items"] >= 3 + 4*Page and X == 70 then
				if Y == 104 then
					Y = 64
				else
					Y = 104
				end
			elseif #Encounter["Items"] >= 4 + 4*Page then
				if Y == 104 then
					Y = 64
				else
					Y = 104
				end
			end
		end
		if Input.Left == 1 and Input.Right <= 0 then
			if X == 70 then
				if #Encounter["Items"] > 4 then
					if Page == 1 then
						Page = 0
						X = 321
					end
					if #Encounter["Items"] >= 4 + 4*Page then
						Dialog("[instant]"..Encounter["Items"][1 + 4*Page].."[MoveTo:351]*"..Encounter["Items"][2 + 4*Page].."\n"..Encounter["Items"][3 + 4*Page].."[MoveTo:351]*"..Encounter["Items"][4 + 4*Page].."\r[MoveTo:351]PAGE "..Page + 1, {X = 131, Y = 120}, nil, true, 1)
					elseif #Encounter["Items"] == 3 + 4*Page then
						Dialog("[instant]"..Encounter["Items"][1 + 4*Page].."[MoveTo:351]*"..Encounter["Items"][2 + 4*Page].."\n"..Encounter["Items"][3 + 4*Page].."\r[MoveTo:351]PAGE "..Page + 1, {X = 131, Y = 120}, nil, true, 1)
					elseif #Encounter["Items"] == 2 + 4*Page then
						Dialog("[instant]"..Encounter["Items"][1 + 4*Page].."[MoveTo:351]*"..Encounter["Items"][2 + 4*Page].."\r\r[MoveTo:351]PAGE "..Page + 1, {X = 131, Y = 120}, nil, true, 1)
					elseif #Encounter["Items"] == 1 + 4*Page then
						Dialog("[instant]"..Encounter["Items"][1 + 4*Page].."\r\r[MoveTo:351]PAGE "..Page + 1, {X = 131, Y = 120}, nil, true, 1)
					end
					DialogUpdate()
					DialogUpdate()
					DialogUpdate()
					DialogUpdate()
				end
			else
				if #Encounter["Items"] >= 2 + 4*Page then
					if Y == 104 then
						X = 70
					elseif Y == 64 and #Encounter["Items"] >= 4 + 4*Page then
						X = 70
					end
				end
			end
		elseif Input.Right == 1 and Input.Left <= 0 then
			if X == 70 then
				if #Encounter["Items"] >= 2 + 4*Page then
					if Y == 104 then
						X = 321
					elseif Y == 64 and #Encounter["Items"] >= 4 + 4*Page then
						X = 321
					end
				end
			else
				if #Encounter["Items"] > 4 then
					if Page == 0 then
						Page = 1
						X = 70
						if #Encounter["Items"] <= 6 then
							Y = 104
						end
					end
					if #Encounter["Items"] >= 4 + 4*Page then
						Dialog("[instant]"..Encounter["Items"][1 + 4*Page].."[MoveTo:351]*"..Encounter["Items"][2 + 4*Page].."\n"..Encounter["Items"][3 + 4*Page].."[MoveTo:351]*"..Encounter["Items"][4 + 4*Page].."\r[MoveTo:351]PAGE "..Page + 1, {X = 131, Y = 120}, nil, true, 1)
					elseif #Encounter["Items"] == 3 + 4*Page then
						Dialog("[instant]"..Encounter["Items"][1 + 4*Page].."[MoveTo:351]*"..Encounter["Items"][2 + 4*Page].."\n"..Encounter["Items"][3 + 4*Page].."\r[MoveTo:351]PAGE "..Page + 1, {X = 131, Y = 120}, nil, true, 1)
					elseif #Encounter["Items"] == 2 + 4*Page then
						Dialog("[instant]"..Encounter["Items"][1 + 4*Page].."[MoveTo:351]*"..Encounter["Items"][2 + 4*Page].."\r\r[MoveTo:351]PAGE "..Page + 1, {X = 131, Y = 120}, nil, true, 1)
					elseif #Encounter["Items"] == 1 + 4*Page then
						Dialog("[instant]"..Encounter["Items"][1 + 4*Page].."\r\r[MoveTo:351]PAGE "..Page + 1, {X = 131, Y = 120}, nil, true, 1)
					end
					DialogUpdate()
					DialogUpdate()
					DialogUpdate()
					DialogUpdate()
				end
			end
		end
	elseif Status == "ActMenu" then
		if (Input.Up == 1 and Input.Down <= 0) or (Input.Down == 1 and Input.Up <= 0) then
			if Y == 104 then
				Y = 64
			else
				Y = 104
			end
		end
		if (Input.Right == 1 and Input.Left <= 0) or (Input.Left == 1 and Input.Right <= 0) then
			if X == 70 then
				X = 321
			else
				X = 70
			end
		end
	elseif Status == "MercyMenu" and Encounter["flee"] then
		if Input.Up == 1 or Input.Down == 1 then
			if Y == 104 then
				Y = 65
			else
				Y = 104
			end
		end
	elseif Status == "Consuming" then
		if Dialogues[1] and not Dialogues[1].Activo then
			timer = timer + 1
		end
		if timer == 1 then
			Bone = CreateSprite("Bullets/Bones/Right Bone v2","Top")
			Bone.rotation = -135
			Bone.MoveTo(710, 104)
			Audio.PlaySound("throw")
		elseif timer > 1 and timer <= 26 then
			Bone.MoveTo(Bone.x - 780/25, 104)
			if Bone.x <= 320 then
				Text1.rotation = 5
				Text2.rotation = -3
				if Bone.x <= 261 then
					Text1.MoveTo(Text1.x - 780/25, 104)
					Text2.MoveTo(Text2.x - 780/25, 104)
				end
			end
		elseif timer == 27 then
			Bone.Remove()
			Text1.Remove()
			Text2.Remove()
			Bone = nil
			Text1 = nil
			Text2 = nil
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Dialog("Oh.[w:10]\nSupongo que no te comiste el\r"..ItemSelected..".", {X = 91, Y = 120}, nil, true, 1)
			else
				Dialog("Oh.[w:10]\nI guess you didn't eat the\r"..ItemSelected..".", {X = 91, Y = 120}, nil, true, 1)
			end
			Status = "Done"
		end
	elseif Status == "Attacking" then
		if not Attacked then
			Target.MoveTo(Target.x - 7.5, 67.5)
			if Target.x <= 317.5 - Arena.width/2 then
				Objetive.Remove()
				Objetive = nil
				Target.Remove()
				Target = nil
				Encounter.Call("MISS")
				Status = "Done"
			elseif Input.Confirm == 1 then
				Target.SetAnimation({"Specials/Target 1","Specials/Target 2"}, 0.08)
				Attacked = true
				timer = 0
			end
		else
			timer = timer + 1
			if timer == 1 then
				Slice = CreateSprite("UI/Battle/spr_slice_o_0","Top")
				Slice.MoveTo(320, 320)
				Slice.loopmode = "ONESHOT"
				Slice.SetAnimation({"UI/Battle/spr_slice_o_0","UI/Battle/spr_slice_o_1","UI/Battle/spr_slice_o_2","UI/Battle/spr_slice_o_3","empty"}, 0.1)
				Audio.PlaySound("slice")
				Encounter.Call("BlockAnim")
			elseif timer == 121 then
				Slice.Remove()
				Slice = nil
				Objetive.Remove()
				Objetive = nil
				Target.Remove()
				Target = nil
				Status = "Done"
			end
		end
	elseif Status == "Flee" then
		timer = timer + 1
		if Encounter["FUN"][1] == "Rare" and Encounter["FUN"][2] == 3 then
			if timer < 300 then
				Flee.x = Flee.x - 1
				if timer == 1 then
					if GetAlMightyGlobal("Idioma") == "Spanish" then
						Dialog("[noskip]Me voy de aquí. . .[w:1000000000] ", {X = 91, Y = 120}, nil, true, 1)
					else
						Dialog("[noskip]I'm out of here. . .[w:1000000000] ", {X = 91, Y = 120}, nil, true, 1)
					end
				elseif timer == 181 then
					if GetAlMightyGlobal("Idioma") == "Spanish" then
						Dialog("[noskip]Espera[w:10], que demo...[w:1000000000] ", {X = 91, Y = 120}, nil, true, 1)
					else
						Dialog("[noskip]Wait[w:10], what the...[w:1000000000] ", {X = 91, Y = 120}, nil, true, 1)
					end
				end
			elseif timer == 361 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Dialog("[noskip]No se suponía que esto...[w:1000000000] ", {X = 91, Y = 120}, nil, true, 1)
				else
					Dialog("[noskip]This wasn't suppose to...[w:1000000000] ", {X = 91, Y = 120}, nil, true, 1)
				end
			elseif timer == 541 then
				Dialog("[noskip]...[w:1000000000] ", {X = 91, Y = 120}, nil, true, 1)
			elseif timer == 661 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Dialog("[noskip]Supongo que no me iré de aquí.", {X = 91, Y = 120}, nil, true, 1)
				else
					Dialog("[noskip]Guess I'm not out of here then.", {X = 91, Y = 120}, nil, true, 1)
				end
			elseif timer == 662 then
				if Dialogues[1] and not Dialogues[1].Activo then
					Heart.Remove()
					Flee.Remove()
					Flee = nil
					Heart = nil
					Dialog("", "", "", "", 1)
					Player.sprite.alpha = 1
					Status = "Done"
				else
					timer = 661
				end
			end
		else
			if timer < 40 then
				Pos.X = Pos.X - 1
				if timer%10 == 5 then
					Heart.Set("HeartFlee 2")
				elseif timer%10 == 0 then
					Heart.Set("HeartFlee 1")
				end
				if timer == 35 then
					table.insert(Bones, {Body = CreateProjectileAbs("Bullets/Bones/Body", -20, 70), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Scale = 120, Vel = 5})
					Audio.PlaySound("pierce")
				end
			elseif timer >= 42 and timer < 44 then
				Pos.X = Pos.X + 5
			elseif timer == 44 then
				for i=1,#Bones do
					local Datos = Bones[i]
					Datos.Vel = 0
				end
				Audio.PlaySound("slam")
				Heart.Set("Undertale/Game Over/Soul")
				Pos.X = 35
			elseif timer >= 45 and timer < 73 then
				Pos.X = math.min(Pos.X + 20, 594)
				for i=2,#ShakeL do
					local Datos = ShakeL[i - tableaux]
					Datos.X = math.min(math.max(Datos.X, Pos.X + 8 + Datos.Sprite.width/2 + 12*(i - 1)), 602 - Datos.Sprite.width/2 - math.min(-19 + 19*(i - 2), 0))
					if timer == 72 then
						for i=1,5 do
							local shard = CreateSprite("Soul Shards/1", "Top")
							shard.SetAnimation({"Soul Shards/1", "Soul Shards/2", "Soul Shards/3", "Soul Shards/4"}, 0.08)
							table.insert(Shards, {Sprite = shard, X = math.random(-40, 40), Y = math.random(60), Pos = {X = Datos.X, Y = Datos.Y}})
						end
						Datos.Sprite.Remove()
						table.remove(ShakeL, i - tableaux)
						tableaux = tableaux + 1
					end
				end
				tableaux = 0
			elseif timer == 73 then
				Audio.PlaySound("roughhit")
			elseif timer >= 74 and timer < 129 then
				Pos.X = Pos.X - 1
				if timer < 113 then
					Pos.Y = 65 + 40*math.sin(math.rad(180/40*(timer - 73)))
				else
					Pos.Y = 65 - 57/15*(timer - 113)
				end
			elseif timer == 214 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[noskip][font:papyrus][voice:mad papyrus]A DÓNDE CREES\nQUE VAS?!", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[noskip][font:papyrus][voice:mad papyrus]WHERE DO YOU\nTHINK YOU ARE\nGOING?!", "rightwide", 490.5, 395, 40, 90, 1)
				end
				Pos.X = 320
				Pos.Y = 78
				for i=1,#ShakeL do
					ShakeL[i - tableaux].Sprite.Remove()
					table.remove(ShakeL, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				for i=1,#Bones do
					local Datos = Bones[i - tableaux]
					Datos.Body.Remove()
					Datos.Head.Remove()
					Datos.Legs.Remove()
					table.remove(Bones, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
			elseif timer == 215 then
				if Bubbles[1] and not Bubbles[1].Activo then
					for i=1,#Shards do
						Shards[i - tableaux].Sprite.StopAnimation()
						Shards[i - tableaux].Sprite.Remove()
						table.remove(Shards, i - tableaux)
						tableaux = tableaux + 1
					end
					tableaux = 0
					Heart.Remove()
					Heart = nil
					Player.sprite.alpha = 1
					Status = "Done"
				else
					timer = timer - 1
				end
			end
			if timer >= 44 and timer < 49 then
				shake = {math.random(-4, 4), math.random(-4, 4)}
			elseif timer >= 73 and timer < 83 then
				shake = {math.random(-10, 10), math.random(-10, 10)}
			end
		end
	end
	if Input.Confirm == 1 and Input.Cancel <= 0 then
		if Status == "Buttons" then
			if X == 41 and Y == 298 then
				X = 70
				Y = 104
				Dialog("[instant]Papyrus", {X = 131, Y = 120}, nil, true, 1)
				Status = "EnemySelect"
				DialogUpdate()
				DialogUpdate()
				DialogUpdate()
				DialogUpdate()
				if not Barra then
					Barra = {HP = CreateSprite("Fake UI, KR and HP Library Sprites/Barra","Top"), Damage = CreateSprite("Fake UI, KR and HP Library Sprites/Barra","Top")}
					Barra.HP.color = {0, 1, 0}
					Barra.HP.Scale(90/10000*Encounter["enemies"][1]["hp"], 20)
					Barra.HP.MoveTo(323 + Barra.HP.xscale/2, 101)
					Barra.Damage.color = {1, 0, 0}
					Barra.Damage.Scale(90 - Barra.HP.xscale, 20)
					Barra.Damage.MoveTo(323 + Barra.HP.xscale + Barra.Damage.xscale/2, 101)
				end
			elseif X == 42 and Y == 238 then
				X = 70
				Y = 104
				Dialog("[instant]Papyrus", {X = 131, Y = 120}, nil, true, 1)
				Status = "EnemyAct"
				DialogUpdate()
				DialogUpdate()
				DialogUpdate()
				DialogUpdate()
			elseif X == 521 and Y == 298 then
				if #Encounter["Items"] > 0 then
					X = 70
					Y = 104
					Page = 0
					if #Encounter["Items"] >= 4 then
						Dialog("[instant]"..Encounter["Items"][1].."[MoveTo:351]*"..Encounter["Items"][2].."\n"..Encounter["Items"][3].."[MoveTo:351]*"..Encounter["Items"][4].."\r[MoveTo:351]PAGE 1", {X = 131, Y = 120}, nil, true, 1)
					elseif #Encounter["Items"] == 3 then
						Dialog("[instant]"..Encounter["Items"][1].."[MoveTo:351]*"..Encounter["Items"][2].."\n"..Encounter["Items"][3].."\r[MoveTo:351]PAGE 1", {X = 131, Y = 120}, nil, true, 1)
					elseif #Encounter["Items"] == 2 then
						Dialog("[instant]"..Encounter["Items"][1].."[MoveTo:351]*"..Encounter["Items"][2].."\r\r[MoveTo:351]PAGE 1", {X = 131, Y = 120}, nil, true, 1)
					elseif #Encounter["Items"] == 1 then
						Dialog("[instant]"..Encounter["Items"][1].."\r\r[MoveTo:351]PAGE 1", {X = 131, Y = 120}, nil, true, 1)
					end
					DialogUpdate()
					DialogUpdate()
					DialogUpdate()
					DialogUpdate()
					Status = "ItemMenu"
				else
					if GetAlMightyGlobal("Idioma") == "Spanish" then
						Dialog("No más puzzles.", {X = 91, Y = 120}, nil, true, 1)
					else
						Dialog("No more puzzles.", {X = 91, Y = 120}, nil, true, 1)
					end
				end
			elseif X == 520 and Y == 238 then
				X = 70
				Y = 104
				if Encounter["flee"] then
					if GetAlMightyGlobal("Idioma") == "Spanish" then
						Dialog("[instant]Perdonar\nHuir", {X = 131, Y = 120}, nil, true, 1)
					else
						Dialog("[instant]Spare\nFlee", {X = 131, Y = 120}, nil, true, 1)
					end
				else
					if GetAlMightyGlobal("Idioma") == "Spanish" then
						Dialog("[instant]Perdonar", {X = 131, Y = 120}, nil, true, 1)
					else
						Dialog("[instant]Spare", {X = 131, Y = 120}, nil, true, 1)
					end
				end
				Status = "MercyMenu"
				DialogUpdate()
				DialogUpdate()
				DialogUpdate()
				DialogUpdate()
			end
			Audio.PlaySound("confirm")
		elseif Status == "EnemySelect" then
			X = -20
			Y = -20
			if Barra then
				Barra.HP.Remove()
				Barra.Damage.Remove()
				Barra = nil
			end
			BtF.Set("Fake UI, KR and HP Library Sprites/FIGHT 0")
			Dialog("", "", "", "", 1)
			Status = "Attacking"
			Objetive = CreateSprite("Specials/Objetivo","Top")
			Objetive.MoveTo(320, 67.5)
			Target = CreateSprite("Specials/Target 1","Top")
			Target.MoveTo(322.5 + Arena.width/2, 67.5)
			Audio.PlaySound("confirm")
		elseif Status == "EnemyAct" then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Dialog("[instant]Checar[MoveTo:351]*Recoquetear\nLlorar[MoveTo:351]*Bromear", {X = 131, Y = 120}, nil, true, 1)
			else
				Dialog("[instant]Check[MoveTo:351]*Reflirt\nCry[MoveTo:351]*Pun", {X = 131, Y = 120}, nil, true, 1)
			end
			Status = "ActMenu"
			Audio.PlaySound("confirm")
			DialogUpdate()
			DialogUpdate()
			DialogUpdate()
			DialogUpdate()
		elseif Status == "ItemMenu" then
			BtI.Set("Fake UI, KR and HP Library Sprites/ITEM 0")
			if X == 70 and Y == 104 then
				ItemSelected = Encounter["Items"][1 + 4*Page]
			elseif X == 321 and Y == 104 then
				ItemSelected = Encounter["Items"][2 + 4*Page]
			elseif X == 70 and Y == 64 then
				ItemSelected = Encounter["Items"][3 + 4*Page]
			elseif X == 321 and Y == 64 then
				ItemSelected = Encounter["Items"][4 + 4*Page]
			end
			if ItemSelected == "[starcolor:7d7d7d][color:7d7d7d]Nothing[color:ffffff]" or ItemSelected == "[starcolor:7d7d7d][color:7d7d7d]Nada[color:ffffff]" then
				Audio.PlaySound("confirm")
			else
				for i=1,#Encounter["Items"] do
					if Encounter["Items"][i] == ItemSelected then
						table.remove(Encounter["Items"], i)
						break
					end
				end
				if ItemSelected == "Pie" then
					ItemSelected = "Butterscotch Pie"
				elseif ItemSelected == "Pay" then
					ItemSelected = "Pay de Caramelo"
				elseif ItemSelected == "SnowPiece" then
					ItemSelected = "Snowman Piece"
				elseif ItemSelected == "P. Nieve" then
					ItemSelected = "Pedazo de Nieve"
				elseif ItemSelected == "L. Hero" then
					ItemSelected = "Legendary Hero"
				elseif ItemSelected == "Héroe L." then
					ItemSelected = "Héroe Legendario"
				elseif ItemSelected == "C. Spaghetti" then
					ItemSelected = "Cold Spaghetti"
				elseif ItemSelected == "Espagueti F." then
					ItemSelected = "Espagueti Frío"
				end
				Inventory.SetInventory(Encounter["Items"])
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Dialog("[noskip][SetSpace:3]Te comiste[func:SpawnText][next]", {X = 91, Y = 120}, nil, true, 1)
				else
					Dialog("[noskip][SetSpace:3]You ate the[func:SpawnText][next]", {X = 91, Y = 120}, nil, true, 1)
				end
				Audio.PlaySound("confirm")
				timer = 0
				X = -20
				Y = -20
				Status = "Consuming"
			end
		elseif Status == "MercyMenu" then
			BtM.Set("Fake UI, KR and HP Library Sprites/MERCY 0")
			Dialog("", "", "", "", 1)
			Audio.PlaySound("confirm")
			if Y == 104 then
				Status = "Done"
			else
				timer = 0
				Player.sprite.alpha = 0
				if Encounter["FUN"][1] == "Rare" and Encounter["FUN"][2] == 3 then
					Heart = CreateSprite("Undertale/Game Over/Soul", "Top")
					Flee = CreateSprite("Flee 1 "..GetAlMightyGlobal("Idioma"),"Top")
					Flee.SetAnimation({"Flee 1 "..GetAlMightyGlobal("Idioma"),"Flee 2 "..GetAlMightyGlobal("Idioma")}, 5/60)
					Flee.MoveTo(139, 66)
				else
					Heart = CreateSprite("HeartFlee 1","Top")
					Spare = CreateSprite("Specials/Spare 2 "..GetAlMightyGlobal("Idioma"), "Top")
					Spare.MoveTo(148, 100)
					table.insert(ShakeL, {Sprite = Spare})
					if GetAlMightyGlobal("Idioma") == "English" then
						Flee = {CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/ast", "Top"), CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/Mayuscula/F", "Top"), CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/Minuscula/l", "Top"), CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/Minuscula/e", "Top"), CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/Minuscula/e", "Top")}
					elseif GetAlMightyGlobal("Idioma") == "Spanish" then
						Flee = {CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/ast", "Top"), CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/Mayuscula/H", "Top"), CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/Minuscula/u", "Top"), CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/Minuscula/i", "Top"), CreateSprite("Dialogs Library Sprites/Box/Fonts/uidialog/Minuscula/r", "Top")}
					end
					local position = 110.5
					for i=1,5 do
						Flee[i].MoveTo(position, 68 - math.max(2 - i, 0))
						position = position + Flee[i].width + 2
						if i == 1 then
							position = position - 10
						end
						table.insert(ShakeL, {Sprite = Flee[i]})
					end
				end
				Heart.color = Player.sprite.color
				Pos = {X = 70, Y = 65}
				Status = "Flee"
				Encounter["flee"] = false
				Audio.PlaySound("flee")
			end
		elseif Status == "ActMenu" then
			BtA.Set("Fake UI, KR and HP Library Sprites/ACT 0")
			if X == 70 and Y == 104 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Dialog("PAPYRUS - 5 ATK 30 DEF[w:10]\nEstá demasiado enojado para\rhablar.", {X = 91, Y = 120}, nil, true, 1)
				else
					Dialog("PAPYRUS - 5 ATK 30 DEF[w:10]\nHe's too angry to talk.", {X = 91, Y = 120}, nil, true, 1)
				end
			elseif X == 321 and Y == 104 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Dialog("Intentas coquetear.[w:10]\nLa expresión de Papyrus hace que\rtodos tus coqueteos se retraigan.", {X = 91, Y = 120}, nil , true, 1)
				else
					Dialog("You try to flirt.[w:10]\nPapyrus's expression makes all\ryour flirts retract.", {X = 91, Y = 120}, nil , true, 1)
				end
			elseif X == 70 and Y == 64 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Dialog("Tu lloras.[w:10]\nPapyrus ya no cree en ti.", {X = 91, Y = 120}, nil , true, 1)
				else
					Dialog("You cry.[w:10]\nPapyrus no longer believes\rin you.", {X = 91, Y = 120}, nil , true, 1)
				end
			elseif X == 321 and Y == 64 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Dialog("Dices la palabra [waitall:5]BROMA[waittall:1].[w:10]\nPapyrus te ignora.", {X = 91, Y = 120}, nil , true, 1)
				else
					Dialog("You say the word [waitall:5]PUN[waittall:1].[w:10]\nPapyrus ignores you.", {X = 91, Y = 120}, nil , true, 1)
				end
			end
			X = -20
			Y = -20
			Audio.PlaySound("confirm")
			Status = "Done"
		end
	elseif Input.Cancel == 1 and Input.Confirm <= 0 then
		if Status == "EnemySelect" then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Dialog("[noskip]No más puzzles.", {X = 91, Y = 120}, nil, true, 1)
			else
				Dialog("[noskip]No more puzzles.", {X = 91, Y = 120}, nil, true, 1)
			end
			Status = "Buttons"
			X = 41
			Y = 298
			if Barra then
				Barra.HP.Remove()
				Barra.Damage.Remove()
				Barra = nil
			end
		elseif Status == "EnemyAct" then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Dialog("[noskip]No más puzzles.", {X = 91, Y = 120}, nil, true, 1)
			else
				Dialog("[noskip]No more puzzles.", {X = 91, Y = 120}, nil, true, 1)
			end
			Status = "Buttons"
			X = 42
			Y = 238
		elseif Status == "ItemMenu" then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Dialog("[noskip]No más puzzles.", {X = 91, Y = 120}, nil, true, 1)
			else
				Dialog("[noskip]No more puzzles.", {X = 91, Y = 120}, nil, true, 1)
			end
			Status = "Buttons"
			X = 521
			Y = 298
		elseif Status == "MercyMenu" then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Dialog("[noskip]No más puzzles.", {X = 91, Y = 120}, nil, true, 1)
			else
				Dialog("[noskip]No more puzzles.", {X = 91, Y = 120}, nil, true, 1)
			end
			Status = "Buttons"
			X = 520
			Y = 238
		elseif Status == "ActMenu" then
			Dialog("[instant]Papyrus", {X = 131, Y = 120}, nil, true, 1)
			Status = "EnemyAct"
			X = 70
			Y = 104
			DialogUpdate()
			DialogUpdate()
			DialogUpdate()
			DialogUpdate()
		end
	end
	Encounter.Call("PapsMoveTo", {310 + (shake[1] or 0), 220 + (shake[2] or 0)})
	Encounter.Call("StatusMoveTo", {350 + (shake[1] or 0), 160.5 + (shake[2] or 0)})
	BtF.MoveTo(80 + (shake[1] or 0), 300 + (shake[2] or 0))
	BtA.MoveTo(80 + (shake[1] or 0), 240 + (shake[2] or 0))
	BtI.MoveTo(560 + (shake[1] or 0), 300 + (shake[2] or 0))
	BtM.MoveTo(560 + (shake[1] or 0), 240 + (shake[2] or 0))
	if Heart then
		Arena.MoveTo(320 + (shake[1] or 0), (shake[2] or 0), false, true)
		Black.MoveTo(320 + (shake[1] or 0), (shake[2] or 0) + 2.5)
		Heart.MoveTo(Pos.X + (shake[1] or 0), Pos.Y + (shake[2] or 0))
	end
	for i=1,#Bones do
		local Datos = Bones[i - tableaux]
		if not Datos.X then
			Datos.X = Datos.Body.absx
		end
		if not Datos.Y then
			Datos.Y = Datos.Body.absy
		end
		Datos.X = Datos.X + Datos.Vel
		Datos.Body.sprite.yscale = Datos.Scale
		Datos.Legs.sprite.rotation = 180
		Datos.Body.MoveToAbs(Datos.X + (shake[1] or 0), Datos.Y + (shake[2] or 0))
		Datos.Head.MoveToAbs(Datos.X + (shake[1] or 0), Datos.Y + Datos.Scale/2 + 2.5 + (shake[2] or 0))
		Datos.Legs.MoveToAbs(Datos.X + (shake[1] or 0), Datos.Y - Datos.Scale/2 - 2.5 + (shake[2] or 0))
	end
	tableaux = 0
	for i=1,#ShakeL do
		local Datos = ShakeL[i]
		if not Datos.X then
			Datos.X = Datos.Sprite.x
		end
		if not Datos.Y then
			Datos.Y = Datos.Sprite.y
		end
		Datos.Sprite.MoveTo(Datos.X + (shake[1] or 0), Datos.Y + (shake[2] or 0))
	end
	for i=1,#Shards do
		local Datos = Shards[i - tableaux]
		Datos.Pos.X = Datos.Pos.X + Datos.X/10
		Datos.Pos.Y = Datos.Pos.Y + Datos.Y/10
		Datos.Sprite.MoveTo(Datos.Pos.X + (shake[1] or 0), Datos.Pos.Y + (shake[2] or 0))
		Datos.Y = Datos.Y - 1
		if Datos.Sprite.y <= -10 then
			Datos.Sprite.Remove()
			table.remove(Shards, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	if Status == "Done" and Dialogues[1] and not Dialogues[1].Activo then
		X = 320
		Y = 78
		Black.Remove()
		BtF.Remove()
		BtA.Remove()
		BtI.Remove()
		BtM.Remove()
		Player.sprite.alpha = 1
		Encounter["nextwaves"] = {"Attack 8"}
		State("DEFENDING")
	end
	if Encounter.GetVar("Textitos") then
		Encounter.SetVar("Textitos", nil)
		Text1 = CreateSprite("Text Part 1 "..GetAlMightyGlobal("Idioma"),"Top")
		Text1.MoveTo(160.5, 104)
		Text2 = CreateSprite("Text Part 2 "..GetAlMightyGlobal("Idioma"),"Top")
		Text2.MoveTo(160.5, 104)
	end
	Player.MoveToAbs(X, Y, true)
end